//
//  ViewController.m
//  Segment
//
//  Created by Scott on 16/5/12.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"
#import "VCOfMessage.h"
#import "VCOfCall.h"

@interface ViewController ()

@property (nonatomic, retain) VCOfMessage *message;

@property (nonatomic, retain) VCOfCall *call;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"消息", @"通话"]];
    segment.selectedSegmentIndex = 0;
    
    
    [segment addTarget:self action:@selector(handleSeg:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segment;
    
    
    self.message = [[VCOfMessage alloc] init];
    self.message.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
    
    [self addChildViewController:self.message];
    [self.view addSubview:self.message.view];
    

}


- (void)handleSeg:(UISegmentedControl *)seg {
    
    switch (seg.selectedSegmentIndex) {
        case 0:
            
            if (!self.message) {
                self.message = [[VCOfMessage alloc] init];
                self.message.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
                
                [self addChildViewController:self.message];
                
                [self.view addSubview:self.message.view];
            } else {
                
                [self.view bringSubviewToFront:self.message.view];
            }
            
            break;
            
            
        case 1:
            if (!self.call) {
                self.call = [[VCOfCall alloc] init];
                self.call.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
                
                [self addChildViewController:self.call];
                [self.view addSubview:self.call.view];
            } else {
                [self.view bringSubviewToFront:self.call.view];
            }
            
            
            
            
            
        default:
            break;
    }
    
    
    
    
    
    
    
}











@end
